﻿//封装到上传图片控件里
function uploadfilejBox(txt) {
    var html = "<table id='uploadbox' width='100%'>";
    html += "<tr><td width='460' height='220'><div id='picPreview'></div></td></tr>";
    html += "<tr><td><a href='javascript:void(0);' class='files'><input type='file' id='FileUpload' name='FileUpload' onchange=SingleUpload('" + txt + "','FileUpload') /></a>";
    html += "<span class='uploading'>正在上传，请稍候...</span></td></tr>";
    html += "</table>";
    $.jBox.open(html, "文件上传", 480, 320, { buttons: { '关闭': true} });
    return false;

}
function openPrejBox(txt) {
    var html = "<table id='uploadbox' width='100%'>";
    html += "<tr><td width='460' height='220'><div id='picPreview'></div></td></tr>";
    html += "</table>";
    $.jBox.open(html, "文件上传", 480, 320, { buttons: { '关闭': true} });
    showpreview($("#" + txt).val());
    return false;

}
function showpreview(src) {
    $("#picPreview").html("<img src='" + src + "' onload='AutoResizeImage(450,210,this)' />");
}

//================ajax上传文件JS函数开始，需和ajaxfileupload.js一起使用===============
//单个文件上传
function SingleUpload(repath, upfileid) {
    $.ajaxFileUpload({
        url: '/Handler/Ajaxfileload.ashx?fileid=' + upfileid,            //需要链接到服务器地址
        secureuri: false,
        fileElementId: upfileid,                        //文件选择框的id属性
        //文件选择框ID 必填
        dataType: 'json',                             //服务器返回的格式，可以是json
        success: function (data, status)              //相当于java中try语句块的用法
        {
            if (data.status == "success") {
                $('#' + repath).val(data.msg);
                $.jBox.success("上传成功", "提示");
                showpreview(data.msg); //加载图片
            } else {
                $.jBox.error("上传失败：" + data.msg, "提示");
            }
        },
        error: function (data, status, e)            //相当于java中catch语句块的用法
        {
            $.jBox.error("上传出错：" + data.msg, "提示");
        }
    });
    return false;
}

//===========================上传文件JS函数结束================================

//图片缩放显示onload='AutoResizeImage(200,100, this)'
function AutoResizeImage(maxWidth, maxHeight, objImg) {
    var img = new Image();
    img.src = objImg.src;
    var hRatio;
    var wRatio;
    var Ratio = 1;
    var w = img.width;
    var h = img.height;
    wRatio = maxWidth / w;
    hRatio = maxHeight / h;
    if (maxWidth == 0 && maxHeight == 0) {
        Ratio = 1;
    } else if (maxWidth == 0) {//
        if (hRatio < 1) Ratio = hRatio;
    } else if (maxHeight == 0) {
        if (wRatio < 1) Ratio = wRatio;
    } else if (wRatio < 1 || hRatio < 1) {
        Ratio = (wRatio <= hRatio ? wRatio : hRatio);
    }
    if (Ratio < 1) {
        w = w * Ratio;
        h = h * Ratio;
    }
    objImg.height = h;
    objImg.width = w;
}
     